package com.raindrop.utils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;

/**
 * Base64Util
 *
 * @author liuhang
 * @date 2018/9/6
 */
public class Base64Util {

    /**
     * 编码算法
     * @param str
     * @return
     */
    public static String encode(String str) {
        return new BASE64Encoder().encode(str.getBytes());
    }

    /**
     * 解码算法
     * @param str
     * @return
     */
    public static String decode(String str) {
        String result = null;
        try {
            BASE64Decoder decoder = new BASE64Decoder();
            result = new String(decoder.decodeBuffer(str));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) {
        String str = "123456";
        String enCodeResult = encode(str);
        System.out.println("enCodeResult = [" + enCodeResult + "]");
        System.out.println("deCodeResult = [" + decode(enCodeResult) + "]");
    }
}
